package com.wct.permission.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author WEI (Email:changtan20000@163.com)
 * @description 获取spring上下文，获取bean
 * @date 2020/1/16 14:11
 */
@Component("applicationContextHelper")
public class ApplicationContextHelper implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        this.applicationContext = context;
    }

    public static <T> T popBean (Class<T> clszz) {
        if (applicationContext == null) {
            return null;
        }

        return applicationContext.getBean(clszz);
    }

    public static <T> T popBean(String name, Class<T> clazz) {
        if (applicationContext == null) {
            return null;
        }

        return applicationContext.getBean(name, clazz);
    }
}
